package com.earnings.okhttputils.utils.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/5/8 0008.
 */

public abstract class YBaseAdapter<T> extends RecyclerView.Adapter {
    private int layoutId;
    private ArrayList<T> list = new ArrayList<>();
    private setOnClickListener ItemOnClickListener;

    public YBaseAdapter(int layoutId) {
        this.layoutId = layoutId;
    }

    public void setDatas(List<T> list) {
        this.list.addAll(list);
        notifyDataSetChanged();
    }
    public void setDatas(int index,List<T> list) {
        this.list.addAll(index,list);
        notifyDataSetChanged();
    }

    public ArrayList<T> getDatas() {
        return list;
    }

    @Override
    public YViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new YViewHolder(LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof YViewHolder) {
            final T data = list.get(position);
            onBind((YViewHolder) holder, position, data);

            if (ItemOnClickListener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ItemOnClickListener.ItemOnClickListener(position, data);
                    }
                });
            }


        }


    }

    public void setOnItemClickListener(setOnClickListener onClickListener) {
        ItemOnClickListener = onClickListener;
    }

    public interface setOnClickListener {
        void ItemOnClickListener(int position, Object data);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public abstract void onBind(YViewHolder holder, int position, T data);

}
